	package Model;

import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;

import MailGenerator.Mailer;
import Model.Wall.Wall;
import Model.OtherUser.OtherUser;
import Model.DateProfile.PersonalInfo;
import Model.InboxMessages.Inbox;
import Model.OtherUser.Blocked;
import Model.DateProfile.PreferenceInfo;
import DBAccess.*;

public class User {

	private int userID;
	private String email;
	private String password;
	private String name;
	private PersonalInfo personalInfo;
	private PreferenceInfo preferenceInfo;
	
	public User(int userID){
		this(userID, false);
	}
	public User(int userID, boolean basic){
		this.userID = userID;
		Statement st=null;
		ResultSet rs=null;
    	try {
    		st = DBAccess.con.createStatement();
    		rs = st.executeQuery("SELECT email, name, pword FROM accountdb WHERE userID="+userID);
    		if (rs.next()){
    			this.email = rs.getString("email");
    			this.setName(rs.getString("name"));
    			//might need to take this out for security reasons and do query directly in sendpass function
    			this.password = rs.getString("pword");
            }
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	DBAccess.close(st, rs);
    	if (!basic)
    		setPersonalInfo(new PersonalInfo(userID));
	}
	
	public static String getName(int userID){
		Statement st=null;
		ResultSet rs=null;
		String name = null;
    	try {
    		st = DBAccess.con.createStatement();
    		rs = st.executeQuery("SELECT name FROM accountdb WHERE userID="+userID);
    		if (rs.next()){
    			name = rs.getString(1);
            }
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	DBAccess.close(st, rs);
    	return name;
	}
	public static String getEmail(int userID){
		Statement st=null;
		ResultSet rs=null;
		String name = null;
    	try {
    		st = DBAccess.con.createStatement();
    		rs = st.executeQuery("SELECT email FROM accountdb WHERE userID="+userID);
    		if (rs.next()){
    			name = rs.getString(1);
            }
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	DBAccess.close(st, rs);
    	return name;
	}
	public static int getId(String n){
		Statement st=null;
		ResultSet rs=null;
		int name = 0;
    	try {
    		st = DBAccess.con.createStatement();
    		rs = st.executeQuery("SELECT userID FROM accountdb WHERE name='"+n+"'");
    		if (rs.next()){
    			name = Integer.parseInt(rs.getString(1));
            }
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	DBAccess.close(st, rs);
    	return name;
	}
	

	public void validate(int userID, String password) {
	}
	
	public String getEmail() {
		return email;
	}
	
	public int getUserID() {
		return userID;
	}
	
	public void emailPassword() {
		Mailer.sendMail(email, "Password recovery", "Your password is "+password+".\nPlease take care not to lose it again.");
	}
	
	public void reportUser(int userIDToReport) {
	}
	
	public void blockUser(int userIDToBlock) {
		boolean inDB=false;
		PreparedStatement st = null;
		ResultSet rs = null;
    	try {
    		st = DBAccess.con.prepareStatement("SELECT userID FROM otherusersdb WHERE userID=?, otherUserID=?");
    		st.setInt(userID, 1);
    		st.setInt(userIDToBlock, 2);
    		rs = st.executeQuery();
    		if(rs.next() && userID==rs.getInt(1)){
    			inDB=true;
    		}
    	} catch (Exception e) {
    		 e.printStackTrace();
    	 }
    	if(inDB)
			DBAccess.updateOtherUser(userID, userIDToBlock, OtherUser.BLOCKED, null);
    	else
    		DBAccess.insertOtherUser(userID, userIDToBlock, OtherUser.BLOCKED, null);
    	DBAccess.close(st,rs);
	}
	
	public void unblockUser(int userIDToUnblock) {
	}
	
	public void updateEmail(String newEmail) {
		DBAccess.updateEmail(newEmail, userID);
	}
	
//	public void updatePassword(String newPassword) {
//		DBAccess.updatePassword(newPassword, userID);
//	}
//	
	public void showUsers(int category) {
	}
	
	public int showFlirt() {
		return 0;
	}
	
	public void sendFlirt(int flirtWith) {
		DBAccess.insertFlirt(flirtWith, userID);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public PreferenceInfo getPreferenceInfo() {
		return preferenceInfo;
	}


	public PersonalInfo getPersonalInfo() {
		return personalInfo;
	}


	public void setPersonalInfo(PersonalInfo myPersonalInfo) {
		this.personalInfo = myPersonalInfo;
	}

	public void setPersonalInfo(PreferenceInfo myPreferenceInfo) {
		this.preferenceInfo = myPreferenceInfo;
	}

}